<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" size="small" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="甲方" prop="partyA.id"
                :rules="[
                  {required: true, message:'甲方不能为空', trigger:'blur'}
                 ]">
          <GridSelect
            title="选择甲方"
            labelName = 'companyName'
            labelValue = 'id'
            :value = "inputForm.partyA.id"
            :limit="1"
            @getValue='(value) => {inputForm.partyA.id=value}'
            :columns="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'companyName',
              label: '企业名称'
            }
            ]"
            :searchs="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'companyName',
              label: '企业名称'
            }
            ]"
            dataListUrl="/dlyrl/hr/enterpriseinformation/enterpriseInformation/list"
            entityBeanName="enterpriseInformation"
            queryEntityUrl="/dlyrl/hr/enterpriseinformation/enterpriseInformation/queryById">
          </GridSelect>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="甲方负责人" prop="partyAuser.id"
                :rules="[
                  {required: true, message:'甲方负责人不能为空', trigger:'blur'}
                 ]">
          <GridSelect
            title="选择甲方负责人"
            labelName = 'companyName'
            labelValue = 'id'
            :value = "inputForm.partyAuser.id"
            :limit="1"
            @getValue='(value) => {inputForm.partyAuser.id=value}'
            :columns="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'companyName',
              label: '企业名称'
            }
            ]"
            :searchs="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'companyName',
              label: '企业名称'
            }
            ]"
            dataListUrl="/dlyrl/hr/enterpriseinformation/enterpriseInformation/list"
            entityBeanName="enterpriseInformation"
            queryEntityUrl="/dlyrl/hr/enterpriseinformation/enterpriseInformation/queryById">
          </GridSelect>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="甲方联系方式" prop="partyAphone"
                :rules="[
                  {required: true, message:'甲方联系方式不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.partyAphone" placeholder="请填写甲方联系方式"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="甲方邮箱" prop="partyAemail"
                :rules="[
                  {required: true, message:'甲方邮箱不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.partyAemail" placeholder="请填写甲方邮箱"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="乙方" prop="partyB.id"
                :rules="[
                  {required: true, message:'乙方不能为空', trigger:'blur'}
                 ]">
          <SelectTree
                      ref="partyB"
                      :props="{
                          value: 'id',             // ID字段名
                          label: 'name',         // 显示名称
                          children: 'children'    // 子级字段名
                        }"

                      url="/sys/office/treeData?type=2"
                      :value="inputForm.partyB.id"
                      :clearable="true"
                      :accordion="true"
                      @getValue="(value) => {inputForm.partyB.id=value}"/>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="乙方负责人" prop="partyBuser.id"
                :rules="[
                  {required: true, message:'乙方负责人不能为空', trigger:'blur'}
                 ]">
                <user-select :limit='1' :value="inputForm.partyBuser.id" @getValue='(value) => {inputForm.partyBuser.id=value}'></user-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="乙方联系方式" prop="partyBphone"
                :rules="[
                  {required: true, message:'乙方联系方式不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.partyBphone" placeholder="请填写乙方联系方式"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="乙方邮箱" prop="partyBemail"
                :rules="[
                  {required: true, message:'乙方邮箱不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.partyBemail" placeholder="请填写乙方邮箱"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="合同金额" prop="contractaMount"
                :rules="[
                  {required: true, message:'合同金额不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.contractaMount" placeholder="请填写合同金额"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="开始时间" prop="beginTime"
                :rules="[
                  {required: true, message:'开始时间不能为空', trigger:'blur'}
                 ]">
                <el-date-picker
                      style="width: 100%;"
                      v-model="inputForm.beginTime"
                      type="datetime"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      placeholder="选择日期时间">
                    </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="结束时间" prop="endTime"
                :rules="[
                  {required: true, message:'结束时间不能为空', trigger:'blur'}
                 ]">
                <el-date-picker
                      style="width: 100%;"
                      v-model="inputForm.endTime"
                      type="datetime"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      placeholder="选择日期时间">
                    </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="签订状态" prop="signedBy"
                :rules="[
                  {required: true, message:'签订状态不能为空', trigger:'blur'}
                 ]">
                <el-select v-model="inputForm.signedBy" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('signed_by')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="签订前附件" prop="notEnclosure"
                :rules="[
                 ]">
              <el-upload ref="notEnclosure"
                    v-if="visible"
                    :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/dlyrl/hr/businesscontract/businessContract`"
                    :headers="{token: $cookie.get('token')}"
                    :on-preview="(file, fileList) => {$window.location.href = (file.response && file.response.url) || file.url}"
                    :on-success="(response, file, fileList) => {
                       inputForm.notEnclosure = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }"
                    :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.notEnclosure = fileList.map(item => item.url).join('|')
                    }"
                    :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }"
                    multiple
                    :limit="5"
                    :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }"
                    :file-list="notEnclosureArra">
                    <el-button size="small" type="primary">点击上传</el-button>
                    <div slot="tip" class="el-upload__tip">添加相关附件</div>
                  </el-upload>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="签订后附件" prop="signEnclosure"
                :rules="[
                 ]">
              <el-upload ref="signEnclosure"
                    v-if="visible"
                    :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/dlyrl/hr/businesscontract/businessContract`"
                    :headers="{token: $cookie.get('token')}"
                    :on-preview="(file, fileList) => {$window.location.href = (file.response && file.response.url) || file.url}"
                    :on-success="(response, file, fileList) => {
                       inputForm.signEnclosure = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }"
                    :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.signEnclosure = fileList.map(item => item.url).join('|')
                    }"
                    :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }"
                    multiple
                    :limit="5"
                    :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }"
                    :file-list="signEnclosureArra">
                    <el-button size="small" type="primary">点击上传</el-button>
                    <div slot="tip" class="el-upload__tip">添加相关附件</div>
                  </el-upload>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="合同预警" prop="earlywarning"
                :rules="[
                 ]">
                <el-select v-model="inputForm.earlywarning" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('warning')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="备注信息" prop="remarks"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"     ></el-input>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="visible = false">关闭</el-button>
      <el-button size="small" type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  import GridSelect from '@/components/gridSelect'
  import SelectTree from '@/components/treeSelect/treeSelect.vue'
  import UserSelect from '@/components/userSelect'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        notEnclosureArra: [],
        signEnclosureArra: [],
        inputForm: {
          id: '',
          partyA: {
            id: ''
          },
          partyAuser: {
            id: ''
          },
          partyAphone: '',
          partyAemail: '',
          partyB: {
            id: ''
          },
          partyBuser: {
            id: ''
          },
          partyBphone: '',
          partyBemail: '',
          contractaMount: '',
          beginTime: '',
          endTime: '',
          notEnclosure: '',
          signEnclosure: '',
          signedBy: '',
          remarks: '',
          earlywarning: ''
        }
      }
    },
    components: {
      GridSelect,
      SelectTree,
      UserSelect
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建商务合同`
        } else if (method === 'edit') {
          this.title = '修改商务合同'
        } else if (method === 'view') {
          this.title = '查看商务合同'
        }
        this.notEnclosureArra = []
        this.signEnclosureArra = []
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/dlyrl/hr/businesscontract/businessContract/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.businessContract)
              this.inputForm.notEnclosure.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.notEnclosureArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                }
              })
              this.inputForm.signEnclosure.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.signEnclosureArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                }
              })
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/dlyrl/hr/businesscontract/businessContract/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>